import axios from 'axios'
import QS from 'qs'
import Conf from '../conf'



axios.defaults.withCredentials = true;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.baseURL = Conf.BASE_API;
axios.defaults.timeout = 5000;


// request拦截器
axios.interceptors.request.use(config => {
	return config
}, error => {
	return Promise.reject(error)
})


axios.interceptors.response.use(response => {
	if (response.status === 200) {
		return Promise.resolve(response);
	} else {
		return Promise.reject(response);
	}
}, error => {
	return Promise.reject(error.response);
});

export function get(url, params) {
	return new Promise((resolve, reject) => {
		axios.get(url, {
			params: params
		}).then(res => {
			resolve(res.data);
		}).catch(err => {
			reject(err.data)
		})
	});
}

export function post(url, params) {
	return new Promise((resolve, reject) => {
		// QS.stringify(params)
		axios.post(url, params)
			.then(res => {
				resolve(res.data);
			})
			.catch(err => {
				reject(err.data)
			})
	});
}

let uploadFileConf = {
	headers: {
		'Content-Type': 'multipart/form-data'
	}
}
export function postFile(url, param) {
	return new Promise((resolve, reject) => {
		axios.post(url, param, uploadFileConf)
			.then(res => {
				resolve(res.data);
			})
			.catch(err => {
				reject(err.data)
			})
	});
}
